package com.qixin.modules.ea.service.impl;

import com.qixin.modules.ea.entity.EaPayment;
import com.qixin.modules.ea.entity.EaBookDetail;
import com.qixin.modules.ea.mapper.EaBookDetailMapper;
import com.qixin.modules.ea.mapper.EaPaymentMapper;
import com.qixin.modules.ea.service.IEaPaymentService;
import com.qixin.modules.rzInvoice.entity.EaBill;
import com.qixin.modules.rzInvoice.mapper.EaBillMapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.nc.service.INcInoutbusiclassService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: ea_payment
 * @Author: qixin-boot
 * @Date:   2022-01-21
 * @Version: V1.0
 */
@Service
public class EaPaymentServiceImpl extends ServiceImpl<EaPaymentMapper, EaPayment> implements IEaPaymentService {

	@Autowired
	private EaPaymentMapper eaPaymentMapper;
	@Autowired
	private EaBookDetailMapper eaBookDetailMapper;
	@Autowired
	private EaBillMapper eaBillMapper;
	@Autowired
	private INcInoutbusiclassService ncInoutbusiclassService;
	
	@Override
	@Transactional
	public void saveMain(EaPayment eaPayment, List<EaBookDetail> eaBookDetailList) {
		// update-begin-by wujinbiao 2022/2/25 16:08 for 把费用项目名称存到数据库表中
		if(StringUtils.isNotBlank(eaPayment.getIndexId())){
			String indexIdName = ncInoutbusiclassService.getIndexIdName(eaPayment.getIndexId());
			eaPayment.setIndexIdName(indexIdName);
		}
		// update-end-by wujinbiao 2022/2/25 16:08 for 把费用项目名称存到数据库表中
		eaPaymentMapper.insert(eaPayment);
		//update-begin-author wjb 2022-02-10 18:43 for 发票被引用后状态修改
		if(eaBookDetailList!=null && eaBookDetailList.size()>0) {
			for(EaBookDetail entity:eaBookDetailList) {
				//更新发票使用情况
				String fpid = entity.getFpid();
				EaBill eaBill = eaBillMapper.selectById(fpid);
				String isused = eaBill.getIsused();   //发票是否已使用
				if(isused.equals("1")){
					throw new RuntimeException("发票号码:" + eaBill.getNumber() + "已被使用!");
				}else{
					eaBill.setIsused("1");  //设置发票已使用
					eaBillMapper.updateById(eaBill);
				}
				//update-end-author wjb 2022-02-10 18:43 for 发票被引用后状态修改
				//外键设置
				entity.setBillId(eaPayment.getId());
				eaBookDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaPayment eaPayment,List<EaBookDetail> eaBookDetailList) {
		// update-begin-by wujinbiao 2022/2/25 16:08 for 把费用项目名称存到数据库表中
		if(StringUtils.isNotBlank(eaPayment.getIndexId())){
			String indexIdName = ncInoutbusiclassService.getIndexIdName(eaPayment.getIndexId());
			eaPayment.setIndexIdName(indexIdName);
		}
		// update-end-by wujinbiao 2022/2/25 16:08 for 把费用项目名称存到数据库表中
		eaPaymentMapper.updateById(eaPayment);

		//update-begin-author wjb 2022-02-10 18:44 for 修改原发票的关联关系
		//1.修改原发票的关联关系
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("bill_id",eaPayment.getId());
		List<EaBookDetail> eaBookDetailList1 = eaBookDetailMapper.selectByMap(paramsMap);
		for(EaBookDetail entity:eaBookDetailList1) {
			String fpid = entity.getFpid();  //发票id
			EaBill eaBill = eaBillMapper.selectById(fpid);
			eaBill.setIsused("0");   //设置状态为未使用
			eaBillMapper.updateById(eaBill);
		}

		//1.先删除子表数据
		eaBookDetailMapper.deleteByMainId(eaPayment.getId());
		
		//2.子表数据重新插入
		if(eaBookDetailList!=null && eaBookDetailList.size()>0) {
			for(EaBookDetail entity:eaBookDetailList) {
				//更新发票使用情况
				String fpid = entity.getFpid();
				EaBill eaBill = eaBillMapper.selectById(fpid);
				String isused = eaBill.getIsused();   //发票是否已使用
				if(isused.equals("1")){
					throw new RuntimeException("发票号码:" + eaBill.getNumber() + "已被使用!");
				}else{
					eaBill.setIsused("1");  //设置发票已使用
					eaBillMapper.updateById(eaBill);
				}
				//update-end-author wjb 2022-02-10 18:44 for 修改原发票的关联关系
				//外键设置
				entity.setBillId(eaPayment.getId());
				eaBookDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		//update-begin-author wjb 2022-02-10 18:40 删除申请单、发票关联关系
		// 费用明细表中的发票改为 未使用
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("bill_id",id);
		List<EaBookDetail> eaBookDetailList1 = eaBookDetailMapper.selectByMap(paramsMap);
		for(EaBookDetail entity:eaBookDetailList1) {
			String fpid = entity.getFpid();  //发票id
			EaBill eaBill = eaBillMapper.selectById(fpid);
			if (eaBill != null){
				eaBill.setIsused("0");   //设置状态为未使用
				eaBillMapper.updateById(eaBill);
			}
		}
		//update-end-author wjb 2022-02-10 18:40 删除申请单、发票关联关系
		eaBookDetailMapper.deleteByMainId(id);
		eaPaymentMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaBookDetailMapper.deleteByMainId(id.toString());
			eaPaymentMapper.deleteById(id);
		}
	}
	
}
